﻿using HomeWork.IBLL;
using HomeWork.IDAL;
using HomeWork.MODEL;

namespace HomeWork.BLL
{
    public class ActionBLL:IActionBLL
    {
        IBaseDal<ActionInfo> dal;
        public ActionBLL(IBaseDal<ActionInfo> dal)
        {
            this.dal = dal;
        }
        /// <summary>
        /// 添加活动
        /// </summary>
        /// <param name="action">实体</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> AddAciton(ActionInfo action)
        {
            try
            {
                var list=await dal.GetAsync(x=>x.ActionName==action.ActionName);
                if (list != null) 
                {
                    return -1;
                }
                return await dal.AddAsync(action);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 活动列表
        /// </summary>
        /// <param name="ActionName">活动名称</param>
        /// <param name="ActionType">活动类型</param>
        /// <returns>符合条件的数据</returns>

        public IQueryable<ActionInfo> GetActionsList(string? ActionName = null, string? ActionType = null)
        {
            try
            {
                var list = dal.GetList().Where(x => x.IsDel == false);
                if(!string.IsNullOrEmpty(ActionName))
                {
                    list=list.Where(x=>x.ActionName.Contains(ActionName));
                }
                if (!string.IsNullOrEmpty(ActionType)) 
                {
                    list=list.Where(x=>x.ActionType==ActionType);
                }
                return list;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 反填活动
        /// </summary>
        /// <param name="ActionId">活动编号</param>
        /// <returns>符合条件的数据</returns>
        public async Task<ActionInfo> FindAction(int ActionId)
        {
            try
            {
                return await dal.GetAsync(x=>x.ActionId== ActionId);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 修改活动
        /// </summary>
        /// <param name="action">实体</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> UpdateAction(ActionInfo action)
        {
            try
            {
                var list=await dal.GetAsync(x=>x.IsDel==false&&x.ActionId==action.ActionId&&x.ActionName==action.ActionName);
                if(list!=null)
                {
                    return -1;
                }
                return await dal.UpdateAsync(action);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}
